<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue'
import { validate } from '@/utils/validate';
import { setToken, setSysToken } from '@/utils/user';
import buildRoute from '@/router/buildRoute';
import { login, getValidateImg } from '@/api/user';

const router = useRouter();

const loading = ref(false);
const formRef = ref();

const formData = reactive({
  username: '',
  password: '',
  code: '',
  id: '',
});
const formRules = reactive({
  username: [{ required: true, message: '必填', trigger: 'change' }],
  password: [{ required: true, message: '必填', trigger: 'change' }],
  code: [{ required: true, message: '必填', trigger: 'change' }],
});

const validateImg = ref();

const getImg = async () => {
  try {
    const { data, code, msg } = await getValidateImg();
    if (code == 200) {
      validateImg.value = data.imageBase64;
      formData.id = data.id;
    }
  } catch (error) {
    console.log(error);
  }
};
getImg();

const loginAction = async () => {
  const valid = await validate(formRef);
  if (valid) {
    handleLogin();
  }
};
const handleLogin = async () => {
  loading.value = true;
  login(formData)
    .then(async (res: any) => {
      setToken(res.data.token);
      setSysToken(res.data.ucToken);
      // await buildRoute();
      router.push('/');
    })
    .catch((err) => {
      getImg();
      formData.code = '';
      ElMessage.error(err.msg);
    })
    .finally(() => {
      loading.value = false;
    });
};
</script>

<template>
  <el-form
    ref="formRef"
    class="login-form"
    :rules="formRules"
    :model="formData"
    size="large"
  >
    <div class="enter-x login-title">安全可靠  智慧物联</div>
    <div class="enter-x login-sub-title">-欢迎登录综合计费管理云平台-</div>
    <el-form-item
      class="enter-x"
      prop="account"
    >
      <el-input
        v-model="formData.username"
        placeholder="请输入用户名或邮箱"
        class="fix-auto-fill"
        autocomplete="off"
        clearable
        :prefix-icon="User"
      />
    </el-form-item>

    <el-form-item class="enter-x" prop="password">
      <el-input
        v-model="formData.password"
        autocomplete="off"
        placeholder="请输入密码"
        type="password"
        show-password
        @keyup.enter.native="loginAction"
        :prefix-icon="Lock"
        clearable
      ></el-input>
    </el-form-item>
    <el-form-item label="" prop="validate" class="enter-x">
      <el-row style="width: 100%; height: 40px">
        <el-col :span="14">
          <el-input v-model="formData.code" placeholder="请输入验证码" />
        </el-col>
        <el-col :span="9" :offset="1">
          <el-image
            style="width: 100%; height: 40px; cursor: pointer"
            :src="validateImg"
            @click="getImg"
          />
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item class="enter-x">
      <el-button
        type="primary"
        class="w-full login-btn"
        :loading="loading"
        @click="loginAction"
      >
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<style lang="scss">
$bg_color: #fff;
$font_color: #666;
.login-form {
  overflow: hidden;
  background-color: #fff;
  padding: 60px;
  .login-title {
    font-size: 24px !important;
    color: #333;
    margin: 0px auto 16px auto;
    font-weight: bold;
    width: 100%;
    text-align: center;
  }
  .login-sub-title {
    font-size: 16px !important;
    color: #838D9A;
    line-height: 26px;
    margin-bottom: 48px;
    font-weight: 400;
    width: 100%;
    text-align: center;
  }
  .account-type {
    padding: 2px 0;
    margin-right: 32px;
    font-size: 16px;
    cursor: pointer;
    position: relative;
    &::after {
      content: "";
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translate(-50%, 0);
      width: 40px;
      border: 1px solid transparent;
    }
    &.active {
      color: #237ae4;
      &::after {
        border-color: #237ae4;
      }
    }
  }
  .other-actions {
    text-align: right;
    padding-right: 10px;
    button {
      color: $font_color;
    }
  }
  .login-btn {
    text-align: center;
    flex: 1;
  }

  .login-btn button {
    width: 100%;
  }
}
</style>
